{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f669a989-2918-4ea2-befe-f153746f7ceb",
   "metadata": {},
   "source": [
    "## reference"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac2aceb8-8831-4f70-82d4-b088f839ada5",
   "metadata": {},
   "source": [
    "- explore the resources that are stored in Orthanc as explained above:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67be6cea-a646-45a9-9113-af3ed0568bb3",
   "metadata": {},
   "source": [
    "curl http://localhost:8042/patients\n",
    "curl http://localhost:8042/studies\n",
    "curl http://localhost:8042/series\n",
    "curl http://localhost:8042/instances"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bc61d457-3644-43db-97ae-fc230e854027",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "sys.path.insert(0,\"/home/biomind/developer/Biomind-Server\")\n",
    "os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'biomind.settings')\n",
    "os.environ[\"DJANGO_ALLOW_ASYNC_UNSAFE\"] = \"true\"\n",
    "from django.core.wsgi import get_wsgi_application\n",
    "application = get_wsgi_application()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75421ce8-cc70-4ea6-986b-390d3aabca1a",
   "metadata": {},
   "source": [
    "## 本地上传数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d4781f38-6467-496a-9f13-9a86d6043ba4",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001b[32m2021-10-08 18:00:44:144\u001b[0m | \u001b[35mMainThread\u001b[0m | \u001b[34m\u001b[1mDEBUG\u001b[0m | \u001b[36mutils.py:191\u001b[0m - \u001b[34m\u001b[1masync_store_instances: storing 2669 instances ...\u001b[0m \n"
     ]
    },
    {
     "ename": "RuntimeError",
     "evalue": "This event loop is already running",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "\u001b[0;32m/tmp/ipykernel_978538/2846053645.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mworkers\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdicom_sender\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mutils\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mPynetDicomSCU\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0mdcm_scu\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mPynetDicomSCU\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mb\"ORTHANC\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"127.0.0.1\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m4242\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34mb\"ORTHANC1084\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mdcm_scu\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0masync_store_instances\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'/home/biomind/data/1632905941.028102.13'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"hw\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m~/developer/Biomind-Server/workers/dicom_sender/utils.py\u001b[0m in \u001b[0;36masync_store_instances\u001b[0;34m(self, dirpath, study_uid)\u001b[0m\n\u001b[1;32m    195\u001b[0m         tasks = [asyncio.ensure_future(self._read_file(x))\n\u001b[1;32m    196\u001b[0m                  for x in dcm_paths if dcm_paths]\n\u001b[0;32m--> 197\u001b[0;31m         \u001b[0mloop\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun_until_complete\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0masyncio\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwait\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtasks\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    198\u001b[0m         \u001b[0mds_list\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mtask\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresult\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mtask\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mtasks\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    199\u001b[0m         \u001b[0mresults\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.biomind/environment/python/3.9.5/lib/python3.9/asyncio/base_events.py\u001b[0m in \u001b[0;36mrun_until_complete\u001b[0;34m(self, future)\u001b[0m\n\u001b[1;32m    616\u001b[0m         \"\"\"\n\u001b[1;32m    617\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_check_closed\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 618\u001b[0;31m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_check_running\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    619\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    620\u001b[0m         \u001b[0mnew_task\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mfutures\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0misfuture\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfuture\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.biomind/environment/python/3.9.5/lib/python3.9/asyncio/base_events.py\u001b[0m in \u001b[0;36m_check_running\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    576\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0m_check_running\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    577\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mis_running\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 578\u001b[0;31m             \u001b[0;32mraise\u001b[0m \u001b[0mRuntimeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'This event loop is already running'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    579\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mevents\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_get_running_loop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    580\u001b[0m             raise RuntimeError(\n",
      "\u001b[0;31mRuntimeError\u001b[0m: This event loop is already running"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/biomind/.local/lib/python3.9/site-packages/pydicom/filereader.py:139: RuntimeWarning: coroutine 'wait' was never awaited\n",
      "  VR = VR.decode(default_encoding)\n",
      "RuntimeWarning: Enable tracemalloc to get the object allocation traceback\n"
     ]
    }
   ],
   "source": [
    "from workers.dicom_sender.utils import PynetDicomSCU\n",
    "dcm_scu = PynetDicomSCU(b\"ORTHANC\", \"127.0.0.1\", 4242, b\"ORTHANC1084\")\n",
    "dcm_scu.async_store_instances('/home/biomind/data/1632905941.028102.13', \"hw\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b22351e2-3cae-42ac-a3b7-4874dbc5cb6a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from fabric2 import Connection\n",
    "from pathlib import Path\n",
    "import tempfile\n",
    "\n",
    "#dcm_path = f\"{CONFIG.orthanc_storage}/StudyInstanceUID/SeriesInstanceUID\"\n",
    "with tempfile.TemporaryDirectory() as tempdir:\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "988a9f6b-dfac-4363-acc6-9d16172040da",
   "metadata": {},
   "outputs": [],
   "source": [
    "from fabric2 import Connection\n",
    "conn = Connection(\"biomind@192.168.1.168\",connect_kwargs={\"password\": \"biomind\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "32a724cc-4bac-4ade-acc4-af4033b6f771",
   "metadata": {},
   "outputs": [],
   "source": [
    "conn"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
